﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ObjectDirectory;
using System.Collections.Generic;

namespace t_ObjectDirectory
{
    [TestClass]
    public class t_ObjectPathManagerOneLevel
    {
        public class Sub
        {
            public int joe;
        }

        public class Container
        {
            public Sub george { get; set; }
        }

        [TestMethod]
        public void GetPathOfGeorge()
        {
            Container c = new Container();
            c.george = new Sub() { joe = 10 };

            ObjectPathManager mgr = new ObjectPathManager();
            Assert.AreEqual("george", mgr.GetPath(c, c.george));
        }

        [TestMethod]
        public void GetGeorgeObject()
        {
            Container c = new Container();
            c.george = new Sub() { joe = 10 };

            ObjectPathManager mgr = new ObjectPathManager();
            Assert.AreEqual(c.george, mgr.FindObjectFromPath(c, "george") as Sub, "Expected to find george through simple lookup");
            Assert.AreEqual(c.george, mgr.FindObjectFromPath(c, "./george") as Sub, "Expected to find george through ./ lookup");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestBadPath()
        {
            Container c = new Container();
            c.george = new Sub() { joe = 10 };

            ObjectPathManager mgr = new ObjectPathManager();
            mgr.FindObjectFromPath(c, "georgee");
        }

        public class bogus
        {
            public double Val{get; set;}
        }

        public class ACollection
        {
            public List<bogus> lotsandLots { get; set; }
            public Container zzzz {get;set;}
        }

        [TestMethod]
        public void TestCollection()
        {
            ACollection col = new ACollection()
            {
                lotsandLots = new List<bogus>() { new bogus(), new bogus(), new bogus() },
                zzzz = new Container() { george = new Sub() { joe = 5 } }
            };

            ObjectPathManager mgr = new ObjectPathManager();
            string path = mgr.GetPath(col, col.zzzz.george);

            Assert.AreEqual("zzzz/george", path, "Didn't find the right path");

            Sub found = mgr.FindObjectFromPath(col, path) as Sub;
            Assert.AreEqual(col.zzzz.george, found, "Found it in the collection");

        }

    }
}